#include "stdafx.h"
#include "CBoldBond.h"


CBoldBond::CBoldBond(CPoint pBegin, CPoint pEnd, COLORREF cPen)
{
	this->m_pBegin	= pBegin;
	this->m_pEnd	= pEnd;
	m_nAngle = NUM_OF_ANGLE;
	m_pCoordinates = new CPoint[NUM_OF_ANGLE];
	this->m_cPenColor = cPen;
	m_brushColor.CreateSolidBrush(RGB(0, 0, 0));
	m_nPenStyle = PS_SOLID;
	m_nPenWidth = 1;
}

BOOL CBoldBond::Draw(CDC *cDC)
{
	radiansOffset = DegreesToRadians(AngleDist() + m_nRotate);
	CPen cPen(m_nPenStyle, m_nPenWidth, m_cPenColor);
	CPen *pOldPen = cDC->SelectObject(&cPen);
	CBrush brushColor (m_clrBack);
	CBrush *OldBrush = cDC->SelectObject(&brushColor);

	this->setCoordinate();
	cDC->Polygon(m_pCoordinates, NUM_OF_ANGLE);

	cDC->SelectObject(pOldPen);
	cPen.DeleteObject();
	cDC->SelectObject (OldBrush);
	brushColor.DeleteObject();
	return	FALSE;
}

CBoldBond::CBoldBond (CPoint m_pBegin, CPoint m_pEnd, COLORREF m_clrBack, COLORREF m_cPenColor, BASESHAPE__T m_nShapeType, INT m_nRotate)
{
	this->m_pBegin = m_pBegin;
	this->m_pEnd = m_pEnd;
	this->m_clrBack = m_clrBack;
	this->m_cPenColor = m_cPenColor;
	this->m_nShapeType = m_nShapeType;
//	this->m_szText = m_szText;
//	this->m_color = m_color;
	m_brushColor.CreateSolidBrush(RGB(123,123,123));
	m_nAngle = NUM_OF_ANGLE;
	m_pCoordinates = new CPoint[NUM_OF_ANGLE];
	m_nPenStyle = PS_SOLID;
	this->m_nRotate = m_nRotate;
	m_nPenWidth = 1;
}

void CBoldBond::Serialize (CArchive &ar)
{
	if (ar.IsStoring() )
	{
		ar << m_pBegin << m_pEnd << m_clrBack << m_cPenColor << m_nShapeType << m_szText << m_color << m_nRotate;
	}
	else
	{
		int nShapeType;
		ar >> m_pBegin >> m_pEnd >> m_clrBack >> m_cPenColor >> nShapeType >> m_szText >> m_color >> m_nRotate;
		m_nShapeType = (BASESHAPE__T)nShapeType;
	}
}

CBoldBond::CBoldBond(void)
{
}

CBoldBond::~CBoldBond(void)
{
}

void CBoldBond::setCoordinate()
{
	double angle;
	const double radius = Radius;
	CPoint Center;
	CPoint Center2;

	Center.x = m_pBegin.x;
	Center.y = m_pBegin.y;

	angle = radiansOffset + 1 * 2 * PI / 3;
	m_pCoordinates[1].x = Center.x + (3) * cos(angle)/* + m_pTT.x*/;
	m_pCoordinates[1].y = Center.y + (3) * sin(angle)/* + m_pTT.y*/;

	angle = radiansOffset + 2 * 2 * PI / 3;
	m_pCoordinates[2].x = Center.x + (3) * cos(angle)/* + m_pTT.x*/;
	m_pCoordinates[2].y = Center.y + (3) * sin(angle)/* + m_pTT.y*/;

	Center2.x = m_pBegin.x - 20 * cos(radiansOffset);
	Center2.y = m_pBegin.y - 20 * sin(radiansOffset);

	angle = radiansOffset + 1 * 2 * PI / 3;
	m_pCoordinates[0].x = Center2.x + (3) * cos(angle);
	m_pCoordinates[0].y = Center2.y + (3) * sin(angle);

	angle = radiansOffset + 2 * 2 * PI / 3;
	m_pCoordinates[3].x = Center2.x + (3) * cos(angle);
	m_pCoordinates[3].y = Center2.y + (3) * sin(angle);

	m_pMax = m_pMin = m_pBegin;
	for(int i = 0; i < m_nAngle; i++)
	{
		m_pMin.x = min(m_pMin.x, m_pCoordinates[i].x);
		m_pMin.y = min(m_pMin.y, m_pCoordinates[i].y);
		m_pMax.x = max(m_pMax.x, m_pCoordinates[i].x);
		m_pMax.y = max(m_pMax.y, m_pCoordinates[i].y);

	}
	
}


int CBoldBond::AngleDist()
{
	//he so goc cua duong thang
	INT A, B; 
	//goc cua duong thang
	float Angle;
	//tinh he so cua duong thang
	A = m_pEnd.y - m_pBegin.y;
	B = m_pBegin.x - m_pEnd.x;
	//tinh goc
	Angle = B / sqrtf((float)A * A + B * B);
	if(m_pEnd.y > m_pBegin.y)
		return 360 - acos(Angle) * 180.0 / PI;
	return acos(Angle) * 180.0 / PI;
}

void CBoldBond::DrawNullRect(CDC *cDC)
{
	const unsigned int Width = 4;
	CRect m_rectT(CPoint(m_pBegin.x - Width, m_pBegin.y - Width), CPoint(m_pBegin.x + Width, m_pBegin.y + Width));
	CPen cPen(m_nPenStyle, 1, m_cPenColor);
	cDC->SelectObject(&cPen);
	cDC->Rectangle(CRect(CPoint(m_pBegin.x - Width, m_pBegin.y - Width), CPoint(m_pBegin.x + Width, m_pBegin.y + Width)));
	cDC->Rectangle(CRect(CPoint(m_pEnd.x - Width, m_pEnd.y - Width), CPoint(m_pEnd.x + Width, m_pEnd.y + Width)));

}
